﻿
using System;
using System.Data;
using System.Collections.Generic;
using EnterpriseFund.Model;

namespace EnterpriseFund.BLL
{
	/// <summary>
	/// SupportPolicy
	/// </summary>
	public partial class SupportPolicy
	{
		private readonly EnterpriseFund.DAL.SupportPolicy dal=new EnterpriseFund.DAL.SupportPolicy();

		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string FPolicyID,decimal FYear)
		{
			return dal.Exists(FPolicyID,FYear);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(EnterpriseFund.Model.SupportPolicy model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(EnterpriseFund.Model.SupportPolicy model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string FPolicyID,decimal FYear)
		{
			
			return dal.Delete(FPolicyID,FYear);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public EnterpriseFund.Model.SupportPolicy GetModel(string FPolicyID, decimal FYear)
		{
			
			return dal.GetModel(FPolicyID, FYear);
		}

		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<EnterpriseFund.Model.SupportPolicy> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<EnterpriseFund.Model.SupportPolicy> DataTableToList(DataTable dt)
		{
			List<EnterpriseFund.Model.SupportPolicy> modelList = new List<EnterpriseFund.Model.SupportPolicy>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				EnterpriseFund.Model.SupportPolicy model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod





		#region  ExtensionMethod


        public DataTable GetInfor(decimal year, string filedOrder = "FTaxClassAllowanceNo,FEnterprisePropNo")
        {
            return dal.GetInfor(string.Format("spYear = {0} ",year), filedOrder);
        }

        public List<EnterpriseFund.Model.SupportPolicy> GetFormatExpression(decimal year)
        {
            var sp = GetModelList(string.Format("sp.FYear = {0} and sp.FProp = '1'", year));

            foreach (var r in sp)
            {
                r.FRuleExpression = r.FRuleExpression.Replace("&&", "AND").Replace("||","OR");
                //todo...
                //if....lese...转换
                //嵌套IF语句未处理
                var computeExpression = r.FComputeExpression; 
                var ifStartIndex = computeExpression.IndexOf("if(");
                if(ifStartIndex >=0)
                {
                    var ifEndIndex = computeExpression.IndexOf("){");
                    var elseStartIndex = computeExpression.IndexOf("}else");
                    var expr = computeExpression.Substring(ifStartIndex + 3, ifEndIndex - ifStartIndex -3);
                    var truepart = computeExpression.Substring(ifEndIndex + 2, elseStartIndex - ifEndIndex -2);
                    var falsepart = computeExpression.Substring(elseStartIndex + 5);
                    r.FComputeExpression = string.Format("IIF({0},{1},{2})", expr, truepart, falsepart);
                }


                //if (r.IsEvalBase)
                //{
                    r.FComputeExpression = r.FComputeExpression.Replace("[rateIn]", r.FInnerRate + "");
                    r.FComputeExpression = r.FComputeExpression.Replace("[rateOut]", r.FOuterRate + "");
                //}
                //else
                //{
                //    var rates = r.FRates.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                //    for (var i = 0; i < rates.Length; i++)
                //    {
                //        r.FComputeExpression = r.FComputeExpression.Replace("[rate" + (i+1) + "]", (decimal.Parse(rates[i]) / 100)+"");
                //    }
                //}
            }

            return sp;
        }

		#endregion  ExtensionMethod
	}
}

